package com.zzsy.carosshopprojects.activitys;

import android.content.Intent;
import android.view.View;

import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.main.HomePageAc;
import com.zzsy.carosshopprojects.activitys.login.LoginAc;
import com.zzsy.carosshopprojects.activitys.login.vmode.LoginMode;
import com.zzsy.carosshopprojects.base.BaseActivity;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.util.ApkInfoUtil;
import com.zzsy.carosshopprojects.util.SPUtil;

import butterknife.BindView;

/***
 * 启动页
 */
public class StartActivity extends BaseActivity {
    private static final int SPLASH_TIME =1500;
    @BindView(R.id.flash_view)
    View mFlashView;

    @Override
    protected int bindLayout() {
        return R.layout.ac_start;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        // StateAppBar.setStatusBarColor(this,

    }

    @Override
    protected void loadMethod() {
        startApp();
    }

    /**
     * 从程序入口 判断是否第一次进入
     */
    private void startApp() {
        if (SPUtil.getInstance().getBoolean(ConfigureFile.ISLOGIN, false)) {
            //已经登录过，是否需要重新登录
            IsAgainLogin();
        } else {
            //还未登录，所以进入登录页面
            handleJump();
        }

    }

    /**
     * 是否需要重新登录
     */
    private void IsAgainLogin() {
        int CurrentVersion = ApkInfoUtil.getVersionCode(StartActivity.this);
        int OldVersion = SPUtil.getInstance().getInt(ConfigureFile.OLDVERSION, 0);
        if (CurrentVersion > OldVersion) {//当前版本与老版本做比较 如果大于 需要重新登录
            LoginMode.builder().setIsFinis(true).setState(LoginMode.UNTREATED).login(StartActivity.this,"oldPwd", "oldPwd");
        } else {//不需要重新登录 已经登录过，直接进入首页
            handleJumpMain();
        }
    }

    /**
     * 跳转到登录页
     */
    private void startLoginAc() {
        startActivity(new Intent(StartActivity.this, LoginAc.class));
        overridePendingTransition(0, 0);
        finish();
    }

    /**
     * 如果不需要重新登录就延迟跳转
     */
    private void handleJump() {
        mFlashView.postDelayed(() -> {
            startLoginAc();
            finish();
        }, SPLASH_TIME);
    }

    /**
     * 直接跳转到主页
     */
    private void handleJumpMain() {
        mFlashView.postDelayed(() -> {
            startActivity(new Intent(StartActivity.this, HomePageAc.class));
            overridePendingTransition(0, 0);
            finish();
//            //SunmiAppStoreUtil.startAppDetail(getApplication(),"com.zzsy.carosshopprojects");
        }, SPLASH_TIME);

    }

}
